/**
 * 上午11:23:23
 */
package com.hsnn.medstgmini.base.std.dao.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.dao.StdDrugcatalogDao;
import com.hsnn.medstgmini.base.std.model.BidDrugcatas;
import com.hsnn.medstgmini.base.std.model.StdDrugQualitySpecLog;
import com.hsnn.medstgmini.base.std.model.StdDrugcatalog;
import com.hsnn.medstgmini.base.std.model.StdDrugcatalogLog;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @author hsnn
 *
 */
@Repository
public class StdDrugcatalogDaoImpl implements StdDrugcatalogDao {

	@Autowired
	private HibernateSqlUtil dao;
	
	
	@Autowired
	private HibernateSqlUtil hibernateSqlUtil;
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	private Session getSession() {
		Session session = sessionFactory.getCurrentSession();
		if (null == session) {
			session = sessionFactory.openSession();
		}
		return session;
	}
	
	
	@Override
	public void save(StdDrugcatalog stdDrugcatalog) {
		StringBuffer  sql = new StringBuffer()
		.append("insert into std_drugcatalog values(?,?,?,?,?,?,?,?,?)");
		Query query = getSession().createSQLQuery(sql.toString());
		query.setParameter(0, stdDrugcatalog.getDrugcatalogId());
		query.setParameter(1, stdDrugcatalog.getDrugcatalogCode());
		query.setParameter(2, stdDrugcatalog.getDrugcatalogName());
		query.setParameter(3, stdDrugcatalog.getStartTime());
		query.setParameter(4, stdDrugcatalog.getEndTime());
		query.setParameter(5, stdDrugcatalog.getStatus());
		query.setParameter(6, stdDrugcatalog.getAttribute());
		query.setParameter(7, stdDrugcatalog.getUpdDatetime());
		query.setParameter(8, stdDrugcatalog.getUpdUser());
		query.executeUpdate();
		getSession().flush();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StdDrugcatalog> getStdDrugcatalogByName(String drugcatalogName) {
		StringBuffer sql = new StringBuffer()
		.append("	select drugcatalog_id drugcatalogId,drugcatalog_code drugcatalogCode,drugcatalog_name drugcatalogName,  ")
		.append("	start_time startTime,end_time endTime,status status,attribute attribute,upd_datetime updDatetime,upd_user updUser ")
		.append("	from std_drugcatalog where drugcatalog_name = :drugcatalogName  ");
		Map<String,String> params = new HashMap<String, String>();
		params.put("drugcatalogName", drugcatalogName);
		List<Map<String,Object>> mapList = (List<Map<String, Object>>) dao.getSqlMapList(sql.toString(), params);
		List<StdDrugcatalog> list = new ArrayList<StdDrugcatalog>();
		if(mapList!=null&&mapList.size()>0){
			for(int i = 0;i<mapList.size();i++){
				mapList.get(i).put("startTime", DateUtil.parseDateTime(mapList.get(i).get("startTime").toString()));
				mapList.get(i).put("endTime", DateUtil.parseDateTime(mapList.get(i).get("endTime").toString()));
				mapList.get(i).put("updDatetime", DateUtil.parseDateTime(mapList.get(i).get("updDatetime").toString()));
				StdDrugcatalog model=new StdDrugcatalog();
		        Field[] field = model.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
		        try {
		            for (int j = 0; j < field.length; j++) { // 遍历所有属性
		                String name1 = field[j].getName(); // 获取属性的名字
		                String name = name1.substring(0, 1).toUpperCase() + name1.substring(1); // 将属性的首字符大写，方便构造get，set方法
		                String type = field[j].getGenericType().toString(); // 获取属性的类型
		                if (type.equals("class java.lang.String")) { // 如果type是类类型，则前面包含"class "，后面跟类名
		                       Method m = model.getClass().getMethod("set"+name,String.class);
		                        if (mapList.get(i).containsKey(name1)) {
		                             m.invoke(model, mapList.get(i).get(name1));
		                        }
		                        
		                }
		            }
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        list.add(model);
			}
		}
		return list;
	}

	@Override
	public void update(StdDrugcatalog stdDrugcatalog) {
		StringBuffer sql = new StringBuffer()
		.append("update std_drugcatalog set drugcatalog_code = ?,drugcatalog_name = ?,start_time = ?,end_time = ?,status = ?,attribute = ?,upd_datetime = ?,upd_user = ? where drugcatalog_id = ?");
		Query query = getSession().createSQLQuery(sql.toString());
		query.setParameter(0, stdDrugcatalog.getDrugcatalogCode());
		query.setParameter(1, stdDrugcatalog.getDrugcatalogName());
		query.setParameter(2, ParseDate.parseFullFormat(stdDrugcatalog.getStartTime()));
		query.setParameter(3, ParseDate.parseFullFormat(stdDrugcatalog.getEndTime()));
		query.setParameter(4, stdDrugcatalog.getStatus());
		query.setParameter(5, stdDrugcatalog.getAttribute());
		query.setParameter(6, ParseDate.parseFullFormat(stdDrugcatalog.getUpdDatetime()));
		query.setParameter(7, stdDrugcatalog.getUpdUser());
		query.setParameter(8, stdDrugcatalog.getDrugcatalogId());
		query.executeUpdate();
		getSession().flush();
	}

	@Override
	public void updateStatus(Pagination page) {
		String array = page.getConditions().get("array");
		String status = page.getConditions().get("status");
		String userId= page.getConditions().get("userId");
		String[] ids = array.split(",");
		List<String> idList = new ArrayList<String>();
	    for( String id : ids ){
	    	idList.add(id);
	    }
	    StringBuffer sql = new StringBuffer()
	    .append("update std_drugcatalog set status = :status,upd_datetime = :updDatetime,upd_user = :updUser where drugcatalog_id in (:idList)");
	    Query query = getSession().createSQLQuery(sql.toString());
	    query.setString("status", status);
	    query.setString("updDatetime",ParseDate.parseFullFormat(new Date()));
	    query.setString("updUser", userId);
	    query.setParameterList("idList", idList);
	    query.executeUpdate();
	    getSession().flush();
	    writeLog(ParseNumber.toInteger(status),idList);
	}
	
	public void writeLog(int status,List<String> idList){
		String tempNew = Constants.ENABLED==status?"启用":"停用";
		String tempOld = Constants.UNENABLED==status?"启用":"停用";
		String detail = "状态:&nbsp;由"+tempOld+"变为"+tempNew;
		StringBuffer sql = new StringBuffer()
		.append("insert into std_drugcatalog_log select uuid(),t.drugcatalog_id,t.drugcatalog_code,t.drugcatalog_name,t.start_time,t.end_time,t.status,t.attribute,:detail,t.upd_datetime,t.upd_user  from std_drugcatalog t where t.drugcatalog_id in (:idList)");
		Query query = getSession().createSQLQuery(sql.toString());
		query.setParameterList("idList", idList);
		query.setParameter("detail", detail);
		query.executeUpdate();
		getSession().flush();
	}
	@Override
	public Pagination getStdDrugcatalogList(Pagination page) {
		String drugcatalogCode = page.getConditions().get("drugcatalogCode");
		String drugcatalogName = page.getConditions().get("drugcatalogName");
		String startTime = page.getConditions().get("startTime");
		String endTime = page.getConditions().get("endTime");
		String status = page.getConditions().get("status");
		String head = "select count(*)"; 
		StringBuffer hql = new StringBuffer()
		.append(" from StdDrugcatalog sd where 1=1 ");
		if(StringUtils.isNotEmpty(drugcatalogCode)){    
			hql.append(" and sd.drugcatalogCode = :drugcatalogCode");
		}
		if(StringUtils.isNotEmpty(drugcatalogName)){
			hql.append(" and sd.drugcatalogName like :drugcatalogName");
		}
		if(StringUtils.isNotEmpty(startTime)  ){
			hql.append(" and date_format(sd.startTime,'%Y-%m-%d') >= :startTime");
		}
		if(StringUtils.isNotEmpty(endTime)  ){
			hql.append(" and date_format(sd.endTime,'%Y-%m-%d') <= :endTime");
		}
		if(StringUtils.isNotEmpty(status)  ){
			hql.append(" and sd.status = :status");
		}
		hql.append(" order by drugcatalogCode desc ");
		Query query = getSession().createQuery(hql.toString());
		Query queryCount = getSession().createQuery(head+(hql.toString()));
		
		if(StringUtils.isNotEmpty(drugcatalogCode)  ){
			query.setParameter("drugcatalogCode",drugcatalogCode.trim() );
			queryCount.setParameter("drugcatalogCode",drugcatalogCode.trim() );
			
		}
		if(StringUtils.isNotEmpty(drugcatalogName)  ){
			query.setParameter("drugcatalogName","%"+drugcatalogName.trim()+"%" );	
			queryCount.setParameter("drugcatalogName","%"+drugcatalogName.trim()+"%" );	
		}
		if(StringUtils.isNotEmpty(startTime)  ){
			query.setParameter("startTime",startTime );	
			queryCount.setParameter("startTime",startTime);	
		}
		if(StringUtils.isNotEmpty(endTime)  ){
			query.setParameter("endTime",endTime );	
			queryCount.setParameter("endTime",endTime);	
		}
		if(StringUtils.isNotEmpty(status)  ){
			query.setParameter("status",Short.parseShort(status) );	
			queryCount.setParameter("status",Short.parseShort(status));	
		}
		Long records = (Long)queryCount.uniqueResult();
		page.setRecords(records);
		query.setFirstResult(page.getFirstResult());
		query.setMaxResults(page.getMaxResults());
		page.setRows(query.list());
		return page;
	}
	/**
	 * 查询药物目录列表
	 */
	@Override
	public Pagination getStdDrugcatalogLogLists(Pagination page) {
		String drugcatalogCode = page.getConditions().get("drugcatalogCode");
		String drugcatalogName = page.getConditions().get("drugcatalogName");
		String qualityspecCode = page.getConditions().get("qualityspecCode");
		String qualityspecName = page.getConditions().get("qualityspecName");
		String startTime = page.getConditions().get("startTime");
		String endTime = page.getConditions().get("endTime");
		String attribute = page.getConditions().get("attribute");
		String status = page.getConditions().get("status");
		
		String head = "select count(*) from ("; 
		StringBuffer hql = new StringBuffer()
		/*.append(" from StdDrugcatalog sd where 1=1 ");*/
		.append(" SELECT sd.drugcatalog_id drugcatalogId,sd.drugcatalog_code drugcatalogCode,sd.drugcatalog_name drugcatalogName,"
				+ "c.qualityspec_code qualityspecCode ,c.qualityspec_name qualityspecName,"
				+ "DATE_FORMAT(sd.start_time,'%Y-%m-%d') startTime,DATE_FORMAT(sd.end_time,'%Y-%m-%d') endTime,"
				+ "sd.attribute attribute,sd.status ")
		.append(" FROM std_drugcatalog sd ")
		.append(" LEFT JOIN bid_drugcatas b ON b.drugcatalog_id = sd.drugcatalog_id ")
		.append(" left join std_drugqualityspec c on c.qualityspec_id = b.qualityspec_id and c.status='1' ")
		.append(" WHERE 1 = 1 ");
		if(StringUtils.isNotEmpty(drugcatalogCode)){
			hql.append(" and sd.drugcatalog_code = :drugcatalogCode");
		}
		if(StringUtils.isNotEmpty(drugcatalogName)){
			hql.append(" and sd.drugcatalog_name like :drugcatalogName");
		}
		if(StringUtils.isNotEmpty(qualityspecCode)  ){
			hql.append(" and c.qualityspec_code = :qualityspecCode");
		}
		if(StringUtils.isNotEmpty(qualityspecName)  ){
			hql.append(" and c.qualityspec_name like :qualityspecName");
		}
		if(StringUtils.isNotEmpty(startTime)  ){
			hql.append(" and sd.start_time >= :startTime");
		}
		if(StringUtils.isNotEmpty(endTime)  ){
			hql.append(" and sd.end_time <= :endTime");
		}
		if(StringUtils.isNotEmpty(attribute)  ){
			hql.append(" and sd.attribute = :attribute");
		}
		if(StringUtils.isNotEmpty(status)  ){
			hql.append(" and sd.status = :status");
		}
		
		hql.append(" order by sd.start_time  desc ");
		Query query = getSession().createSQLQuery(hql.toString());
		Query queryCount = getSession().createSQLQuery(head+(hql.toString())+") ct");
		
		if(StringUtils.isNotEmpty(drugcatalogCode)  ){
			query.setParameter("drugcatalogCode",drugcatalogCode.trim() );
			queryCount.setParameter("drugcatalogCode",drugcatalogCode.trim() );
			page.getConditions().put("drugcatalogCode", drugcatalogCode);
			
		}
		if(StringUtils.isNotEmpty(drugcatalogName)  ){
			query.setParameter("drugcatalogName","%"+drugcatalogName.trim()+"%" );	
			queryCount.setParameter("drugcatalogName","%"+drugcatalogName.trim()+"%" );	
			page.getConditions().put("drugcatalogName","%"+drugcatalogName.trim()+"%" );
			
		}
		if(StringUtils.isNotEmpty(qualityspecCode)  ){
			query.setParameter("qualityspecCode",qualityspecCode.trim() );	
			queryCount.setParameter("qualityspecCode",qualityspecCode.trim() );
			page.getConditions().put("qualityspecCode",qualityspecCode);	
		}
		if(StringUtils.isNotEmpty(qualityspecName)  ){
			query.setParameter("qualityspecName","%"+qualityspecName.trim()+"%" );	
			queryCount.setParameter("qualityspecName","%"+qualityspecName.trim()+"%" );	
			page.getConditions().put("qualityspecName","%"+qualityspecName+"%" );
		}
		if(StringUtils.isNotEmpty(startTime)  ){
			query.setParameter("startTime",startTime+"00:00:00");	
			queryCount.setParameter("startTime",startTime+"00:00:00");	
			page.getConditions().put("startTime",startTime+"00:00:00");	
		}
		if(StringUtils.isNotEmpty(endTime)  ){
			query.setParameter("endTime",endTime+"23:59:59");	
			queryCount.setParameter("endTime",endTime+"23:59:59");	
			page.getConditions().put("endTime",endTime+"23:59:59");	 
		}
		if(StringUtils.isNotEmpty(attribute)  ){
			query.setParameter("attribute",Short.parseShort(attribute) );	
			queryCount.setParameter("attribute",attribute);
			page.getConditions().put("attribute",attribute);	
		}
		if(StringUtils.isNotEmpty(status)  ){
			query.setParameter("status",Short.parseShort(status) );	
			queryCount.setParameter("status",Short.parseShort(status));
			page.getConditions().put("status",status);	
		}
		return dao.getSqlPageMapList(hql.toString(), page.getConditions(), page);
	}

	@Override
	public Pagination getStdDrugcatalogLogList(Pagination page) {
		String startDate = page.getConditions().get("startDate");
		String endDate = page.getConditions().get("endDate");
		StringBuffer sql = new StringBuffer()
		.append(" select su.name updUser, ")
		.append(" sdl.log_id logId, ")
		.append(" sdl.detail , ")
		.append(" DATE_FORMAT(sdl.upd_datetime,'%Y-%m-%d %T')  updDatetime ")
		.append(" from std_drugcatalog_log sdl left join sys_user su on sdl.upd_user = su.id ")
		.append(" where sdl.drugcatalog_id=:drugcatalogId ");
		if(StringUtils.isNotBlank(startDate)){
			sql.append(" and :startDate <=  DATE_FORMAT(sdl.upd_datetime,'%Y-%m-%d') ");
		}
		if(StringUtils.isNotBlank(endDate)){
			sql.append(" and :endDate >= DATE_FORMAT(sdl.upd_datetime,'%Y-%m-%d') ");
		}
		sql.append(" order by sdl.upd_datetime desc ");
		return dao.getSqlPageMapList(sql.toString(), page.getConditions(), page);
	}



	@Override
	public void update(Object entity) {
		getSession().update(entity);
		getSession().flush();
	}


	
	@Override
	public Object getUniquerResult(Class<?> entity, String key,String value) {
		Criteria criteria = getSession().createCriteria(entity);
		criteria.add(Restrictions.eq(key, value));
		return criteria.uniqueResult();
	}


	/**
	 * @category 获取药物目录详情数据
	 * @author 应晓川
	 * @date 2015年8月19日08:41:15
	 * @return
	 * @return Pagination
	 */
	@Override
	public Pagination getStdDrugcatalogDetailInfo(Pagination page) {
		Map<String,String> params = new HashMap<String, String>();
		String qualityspecCode = page.getConditions().get("qualityspecCode");
		String qualityspecName = page.getConditions().get("qualityspecName");
		String qualityspecClass = page.getConditions().get("qualityspecClass");
		String qualityspecIndex = page.getConditions().get("qualityspecIndex");//
		String status = page.getConditions().get("status");
		String drugcatalogId = page.getConditions().get("drugcatalogId");
		
		StringBuffer sql = new StringBuffer()
		.append(" select sdqs.qualityspec_id qualityspecId, ")
		.append(" sdqs.qualityspec_code qualityspecCode, ")
		.append(" sdqs.qualityspec_name qualityspecName, ")
		.append(" sdqs.form_name formName, ")
		.append(" sdqs.spec_name specName, ")
		.append(" bd.qualityspec_class qualityspecClass, ")
		.append(" bd.drug_cata_id drugCataId, ")
		.append(" bd.qualityspec_index qualityspecIndex, ")
		.append(" bd.status  status ")
		.append(" from bid_drugcatas bd  LEFT JOIN std_drugqualityspec sdqs ON bd.qualityspec_id = sdqs.qualityspec_id ")
		.append(" WHERE bd.drugcatalog_id = :drugcatalogId");
		params.put("drugcatalogId", drugcatalogId);
		if (StringUtils.isNotBlank(qualityspecCode)){
			//sql.append(" AND sdqs.qualityspec_code =:qualityspecCode ");
			params.put("qualityspecCode", qualityspecCode);
		}
		if (StringUtils.isNotBlank(qualityspecName)){
			sql.append(" AND (sdqs.qualityspec_name like :qualityspecName OR sdqs.qualityspec_name_spel like :qualityspecName OR sdqs.qualityspec_name_eng like :qualityspecName  )");
			//page.getConditions().put("qualityspecName", "%"+qualityspecName+"%");
			params.put("qualityspecName", "%"+qualityspecName+"%");
		}
		if (StringUtils.isNotBlank(qualityspecClass)){
			sql.append(" AND bd.qualityspec_class like :qualityspecClass ");
		//	page.getConditions().put("qualityspecClass", "%"+qualityspecClass+"%");
			params.put("qualityspecClass", "%"+qualityspecClass+"%");
		}
		if (StringUtils.isNotBlank(qualityspecIndex)){
			sql.append(" AND bd.qualityspec_index = :qualityspecIndex ");
			params.put("qualityspecIndex", qualityspecIndex);
		}
		if (StringUtils.isNotBlank(status)){
			sql.append(" AND bd.status = :status ");
			params.put("status", status);
		}
		sql.append(" order by bd.upd_datetime desc ");
		return dao.getSqlPageMapList(sql.toString(), params, page);
		
	}


	/**
	 * @category 获取未关联药物目录的品规数据
	 * @author 应晓川
	 * @date 2015年8月19日08:49:53
	 * @return
	 * @return Pagination
	 */
	@Override
	public Pagination getStdDrugcatalogDetailInfoAdd(Pagination page) {
		Map<String,String> params = new HashMap<String, String>();
		String qualityspecCode = page.getConditions().get("qualityspecCode");
		String qualityspecName = page.getConditions().get("qualityspecName");
		String drugcatalogId = page.getConditions().get("drugcatalogId");
		StringBuffer sql = new StringBuffer()
		.append(" select sdqs.qualityspec_id qualityspecId, ")
		.append(" sdqs.qualityspec_code qualityspecCode, ")
		.append(" sdqs.qualityspec_name qualityspecName, ")
		.append(" sdqs.form_name formName, ")
		.append(" sdqs.spec_name specName ")
		.append(" from std_drugqualityspec sdqs LEFT JOIN (select drug_cata_id,qualityspec_id,drugcatalog_id FROM bid_drugcatas WHERE drugcatalog_id = :drugcatalogId) bd  ON bd.qualityspec_id = sdqs.qualityspec_id ")
		.append("  WHERE bd.qualityspec_id is null AND sdqs.status =1 ");//品规状态为有效的
		params.put("drugcatalogId", drugcatalogId);
		if (StringUtils.isNotBlank(qualityspecCode)){
			sql.append(" AND sdqs.qualityspec_code =:qualityspecCode ");
		//	page.getConditions().put("qualityspecCode", qualityspecCode);
			params.put("qualityspecCode", qualityspecCode);
		}
		if (StringUtils.isNotBlank(qualityspecName)){
			sql.append(" AND (sdqs.qualityspec_name like :qualityspecName OR sdqs.qualityspec_name_spel like :qualityspecName OR sdqs.qualityspec_name_eng like :qualityspecName  )");
		//	page.getConditions().put("qualityspecName", "%"+qualityspecName+"%");
			params.put("qualityspecName", "%"+qualityspecName+"%");
		}
		
		return dao.getSqlPageMapList(sql.toString(),params, page);
		//page.setRows(dao.getSqlMapList(sql.toString(), params));
		//return page;
		
	}


	/**
	 * @category 添加明细
	 * @author 应晓川
	 * @param page
	 * @return
	 */
	@Override
	public void AddStdDrugcatalogDetailInfo(Pagination page) {
	    
	}


	@SuppressWarnings("unchecked")
	@Override
	public StdDrugcatalogLog compareStdDrugcatalogLog(StdDrugcatalogLog stdDrugcatalogLog) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer()
		.append(" from StdDrugcatalogLog where 1=1 ");
		if(StringUtils.isNotBlank(stdDrugcatalogLog.getDrugcatalogId())){
			sql.append(" AND drugcatalogId = :drugcatalogId");
		}
		if(StringUtils.isNotBlank(DateUtil.dateTime(stdDrugcatalogLog.getUpdDatetime()))){
			sql.append(" AND updDatetime <:updDatetime");
		}
		sql.append(" order by updDatetime DESC ");
		Query query = session.createQuery(sql.toString())
		.setFirstResult(0).setMaxResults(1);
		if(StringUtils.isNotBlank(stdDrugcatalogLog.getDrugcatalogId())){
			query.setParameter("drugcatalogId", stdDrugcatalogLog.getDrugcatalogId());
		}
		if(StringUtils.isNotBlank(DateUtil.dateTime(stdDrugcatalogLog.getUpdDatetime()))){
			query.setParameter("updDatetime", stdDrugcatalogLog.getUpdDatetime());
		}
		List<StdDrugcatalogLog> list = query.list();
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
        
	}


	@Override
	public String getNameByUpdUser(String updUser) {
		Query query = getSession().createSQLQuery(" select name from sys_user where id =:updUser ");
		String name =  query.setParameter("updUser", updUser).uniqueResult().toString();
		return name;
	}
	
	/**
	 * @category 更新药品目录关联表
	 * @author zhouqi
	 * @date 2015年9月24日09:57:34
	 * @param bidDrugCates
	 */
	@Override
	public void updateBidDrugCatas(BidDrugcatas bidDrugCates){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(bidDrugCates);
		session.flush();
		session.clear();
	}
	
	/**
	 * @category 获取药品目录关联信息
	 * @author zhouqi
	 * @date 2015年9月24日09:57:34
	 * @param bidDrugCates
	 */
	@Override
	public BidDrugcatas getBidDrugCatas(String bidDrugCatesId){
		Session session = sessionFactory.getCurrentSession();
		return (BidDrugcatas) session.get(BidDrugcatas.class, bidDrugCatesId);
	}
	
	/**
	 * 根据条件获取不分页数据
	 * @param page
	 * @return page
	 */
	@Override
	public Pagination getDrugcatalogDetailNoPage(Pagination page){
		Map<String,String> params = new HashMap<String, String>();
		String qualityspecName = page.getConditions().get("qualityspecName");
		String qualityspecClass = page.getConditions().get("qualityspecClass");
		String qualityspecIndex = page.getConditions().get("qualityspecIndex");//
		String qualityspecStatus = page.getConditions().get("qualityspecStatus");
		String drugcatalogId = page.getConditions().get("drugcatalogId");
		
		StringBuffer sql = new StringBuffer()
		.append(" select  ")
		.append(" sdqs.qualityspec_code qualityspecCode, ")
		.append(" sdqs.qualityspec_name qualityspecName, ")
		.append(" sdqs.form_name formName, ")
		.append(" sdqs.spec_name specName, ")
		.append(" bd.qualityspec_class qualityspecClass, ")
		.append(" bd.drug_cata_id drugCataId, ")
		.append(" bd.qualityspec_index qualityspecIndex, ")
		.append(" case bd.status when 0 then '停用' else '启用' end status ")
		.append(" from bid_drugcatas bd  LEFT JOIN std_drugqualityspec sdqs ON bd.qualityspec_id = sdqs.qualityspec_id ")
		.append(" WHERE bd.drugcatalog_id = :drugcatalogId");
		params.put("drugcatalogId", drugcatalogId);
		if (StringUtils.isNotBlank(qualityspecName)){
			sql.append(" AND (sdqs.qualityspec_name like :qualityspecName OR sdqs.qualityspec_name_spel like :qualityspecName OR sdqs.qualityspec_name_eng like :qualityspecName  )");
			params.put("qualityspecName", "%"+qualityspecName+"%");
		}
		if (StringUtils.isNotBlank(qualityspecClass)){
			sql.append(" AND bd.qualityspec_class like :qualityspecClass ");
			params.put("qualityspecClass", "%"+qualityspecClass+"%");
		}
		if (StringUtils.isNotBlank(qualityspecIndex)){
			sql.append(" AND bd.qualityspec_index = :qualityspecIndex ");
			params.put("qualityspecIndex", qualityspecIndex);
		}
		if (StringUtils.isNotBlank(qualityspecStatus)){
			sql.append(" AND bd.status = :qualityspecStatus ");
			params.put("qualityspecStatus", qualityspecStatus);
		}
		sql.append(" order by bd.upd_datetime desc ");
		page.setRows(dao.getSqlMapList(sql.toString(), params));
		return page;
	}
	
	
	/**
	 * 根据条件获取不分页数据
	 * @param page
	 * @return page
	 */
	@Override
	public Pagination getDrugcatalogDetailNoPages(Pagination page){
		Map<String,String> params = new HashMap<String, String>();
		String qualityspecCode = page.getConditions().get("qualityspecCode");
		String qualityspecName = page.getConditions().get("qualityspecName");
		String qualityspecClass = page.getConditions().get("qualityspecClass");
		String qualityspecIndex = page.getConditions().get("qualityspecIndex");//
		String qualityspecStatus = page.getConditions().get("qualityspecStatus");
		String drugcatalogId = page.getConditions().get("drugcatalogId");
		
		StringBuffer sql = new StringBuffer()
		.append(" select  ")
		.append(" sdqs.qualityspec_code qualityspecCode, ")
		.append(" sdqs.qualityspec_name qualityspecName, ")
		.append(" sdqs.form_name formName, ")
		.append(" sdqs.spec_name specName, ")
		.append(" bd.qualityspec_class qualityspecClass, ")
		.append(" bd.drug_cata_id drugCataId, ")
		.append(" bd.qualityspec_index qualityspecIndex, ")
		.append(" case bd.status when 0 then '停用' else '启用' end status ")
		.append(" from bid_drugcatas bd  LEFT JOIN std_drugqualityspec sdqs ON bd.qualityspec_id = sdqs.qualityspec_id ")
		.append(" WHERE bd.drugcatalog_id = :drugcatalogId");
		params.put("drugcatalogId", drugcatalogId);
		if (StringUtils.isNotBlank(qualityspecCode)){
			sql.append(" AND sdqs.qualityspec_code = :qualityspecCode ");
			params.put("qualityspecCode", qualityspecCode);
		}
		if (StringUtils.isNotBlank(qualityspecName)){
			sql.append(" AND (sdqs.qualityspec_name like :qualityspecName OR sdqs.qualityspec_name_spel like :qualityspecName OR sdqs.qualityspec_name_eng like :qualityspecName  )");
			params.put("qualityspecName", "%"+qualityspecName+"%");
		}
		if (StringUtils.isNotBlank(qualityspecClass)){
			sql.append(" AND bd.qualityspec_class like :qualityspecClass ");
			params.put("qualityspecClass", "%"+qualityspecClass+"%");
		}
		if (StringUtils.isNotBlank(qualityspecIndex)){
			sql.append(" AND bd.qualityspec_index = :qualityspecIndex ");
			params.put("qualityspecIndex", qualityspecIndex);
		}
		if (StringUtils.isNotBlank(qualityspecStatus)){
			sql.append(" AND bd.status = :qualityspecStatus ");
			params.put("qualityspecStatus", qualityspecStatus);
		}
		sql.append(" order by bd.upd_datetime desc ");
		page.setRows(dao.getSqlMapList(sql.toString(), params));
		return page;
	}


	/**
	 * 品规库维护记录详情
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getStdDrugcatalogLogDetail(StdDrugcatalogLog stdDrugcatalogLog) {
		StringBuffer  sql = new StringBuffer();
		sql.append(" SELECT s.drugcatalog_id drugcatalogId,s.drugcatalog_code drugcatalogCode,s.drugcatalog_name drugcatalogName,  ")
		   .append(" DATE_FORMAT(s.start_time,'%Y-%m-%d ')  startTime,DATE_FORMAT(s.end_time,'%Y-%m-%d ') endTime,  ")
		   .append(" s.attribute attribute,s.status status ,DATE_FORMAT(s.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,b.name updUser  ")
		   .append(" FROM std_drugcatalog_log s  left join sys_user b on b.id=s.upd_user ")
		   .append(" WHERE s.drugcatalog_id = (SELECT drugcatalog_id FROM std_drugcatalog_log WHERE log_id = :logId)  ")
		   .append(" AND s.upd_datetime <= (SELECT upd_datetime FROM std_drugcatalog_log where log_id = :logId)   ")
		   .append(" order by s.upd_datetime desc limit 2 ");
		Map<String,String> params = new HashMap<String,String>();
		params.put("logId", stdDrugcatalogLog.getLogId());
		List<Map<String,Object>> list = (List<Map<String,Object>>) hibernateSqlUtil.getSqlMapList(sql.toString(), params);
		return list;
	}
	
	
	

}
